﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace BLL.Business
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserBLL
    {
        DAL.DataOperate.UserDAL userDAL = new DAL.DataOperate.UserDAL();

        public DataSet SelectAll()
        {
            return userDAL.SelectAll();
        }

        public DataSet SelectByName(String UserName)
        {
            if (UserName.ToString() == "" || UserName.ToString() == null)
                return null;
            else
               return userDAL.SelectByName(UserName);
        }

        public DataSet SelectByID(int UserId)
        {
            if (UserId.ToString() == "" || UserId.ToString() == null)
                return null;
            else
                return userDAL.SelectByID(UserId);
        }
        /// <summary>
        ///  判断员用户存在否
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns>不存在：false
        ///          存在：  true</returns>
        public bool IsExist(String UserName)
        {
            DataSet ds = new DataSet();
            ds = SelectByName(UserName);

            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                return false;
            }
            else
                return true;
        }
        public bool LoginSucess(String UserName, String UserPassword)
        {
            DataSet ds = new DataSet();
            String password = "";

            if (!IsExist(UserName))
            {
                return false;
            }
            else
            {
                ds = SelectByName(UserName);
                if ((ds == null) ||
                    (ds.Tables.Count == 0) ||
                    (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                {
                    return false;
                }
                else
                {
                    password = ds.Tables[0].Rows[0].ItemArray[1].ToString();
                    if (password == UserPassword)
                    {
                        return true;
                    }
                    else
                        return false;
                }
            }
        }

        public bool Insert(Model.User user)
        {
            if (user.User_Passwordstr == "" ||
                user.User_Namestr == "" ||
                user.User_Mailstr == "" ||
                user.User_Telephonestr == "")
            {
                return false; ;
            }
            else
            {
                if (user.User_Questionstr == "")
                    user.User_Answerstr = "";
                if (!IsExist(user.User_Namestr)) // 如果该用户名没有被注册 
                {
                    return userDAL.Insert(user);
                }
                else
                {
                    return false;
                }
            }
        }
        public bool Update(Model.User user)
        {
            if (user.User_Idint.ToString() == "" ||
                user.User_Namestr == "" ||
                user.User_Mailstr == "" ||
                user.User_Telephonestr == "")
            {
                return false; ;
            }
            else
            {
                if (user.User_Questionstr == "")
                    user.User_Answerstr = "";
                return userDAL.Update(user);
            }
        }
        public bool UpdatePassword(String UserName, String oldPass, String newPass)
        {
            if ((UserName == "" || UserName == null) ||
                (oldPass == "" || oldPass == null) ||
                (newPass == "" || newPass == null))
            {
                return false;
            }
            else
            {
                return userDAL.UpdatePassword(UserName,oldPass,newPass);
            }
        }
        public bool Delete(int UserId)
        {
            if (UserId.ToString() == "" || UserId.ToString() == null)
            {
                return false;
            }
            else
            {
                return userDAL.Delete(UserId);
            }
        }
    }
}
